<template>
  <div>
    <div class="gva-form-box">
      <el-form :model="formData" ref="elFormRef" label-position="right" :rules="rule" label-width="80px">
        <el-form-item label="影片名:" prop="name">
          <el-input v-model="formData.name" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="竖版封面内容md5:" prop="imgUrl">
          <SelectImage v-model="formData.imgUrl" file-type="image"/>
       </el-form-item>
        <el-form-item label="横版封面内容md5:" prop="bannerImgUrl">
          <SelectImage v-model="formData.bannerImgUrl" file-type="image"/>
       </el-form-item>
        <el-form-item label="作者:" prop="author">
          <el-input v-model="formData.author" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="总集数:" prop="totalCnt">
          <el-input v-model.number="formData.totalCnt" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="最大免费集数:" prop="maxFreeCnt">
          <el-input v-model.number="formData.maxFreeCnt" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="每单集价格:" prop="price">
          <el-input v-model.number="formData.price" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="是否完结：1、完结，2、连载:" prop="fullFlag">
          <el-select v-model="formData.fullFlag" placeholder="请选择" :clearable="true">
            <el-option v-for="(item,key) in finishStatusOptions" :key="key" :label="item.label" :value="item.value" />
          </el-select>
       </el-form-item>
        <!-- <el-form-item label="合作方id:" prop="cpId">
          <el-input v-model.number="formData.cpId" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="CP更新日期:" prop="cpUpdateTime">
          <el-date-picker v-model="formData.cpUpdateTime" type="date" placeholder="选择日期" :clearable="true"></el-date-picker>
       </el-form-item> -->
        <el-form-item label="主角名:" prop="protagonist">
          <el-input v-model="formData.protagonist" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="标签:" prop="tag">
          <el-input v-model="formData.tag" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="简介:" prop="intro">
          <el-input v-model="formData.intro" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="推荐语:" prop="recommend">
          <el-input v-model="formData.recommend" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="备注:" prop="remarks">
          <el-input v-model="formData.remarks" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="状态:1、上架，2、下架:" prop="status">
          <el-select v-model="formData.status" placeholder="请选择" :clearable="true">
            <el-option v-for="(item,key) in shelvesOptions" :key="key" :label="item.label" :value="item.value" />
          </el-select>
       </el-form-item>
        <el-form-item label="第一集id:" prop="firstSid">
          <el-input v-model.number="formData.firstSid" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="最新集信息id<>:name<>:ctime:" prop="latestEpisode">
          <el-input v-model="formData.latestEpisode" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="创建人:" prop="createUid">
          <el-input v-model.number="formData.createUid" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="创建时间:" prop="createTime">
          <el-date-picker v-model="formData.createTime" type="date" placeholder="选择日期" :clearable="true"></el-date-picker>
       </el-form-item>
        <el-form-item label="更新人:" prop="updateUid">
          <el-input v-model.number="formData.updateUid" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="更新时间:" prop="updateTime">
          <el-date-picker v-model="formData.updateTime" type="date" placeholder="选择日期" :clearable="true"></el-date-picker>
       </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="save">保存</el-button>
          <el-button type="primary" @click="back">返回</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import {
  createMovieAuto,
  updateMovieAuto,
  findMovieAuto
} from '@/api/movieAuto'

defineOptions({
    name: 'MovieAutoForm'
})

// 自动获取字典
import { getDictFunc } from '@/utils/format'
import { useRoute, useRouter } from "vue-router"
import { ElMessage } from 'element-plus'
import { ref, reactive } from 'vue'
import SelectImage from '@/components/selectImage/selectImage.vue'

const route = useRoute()
const router = useRouter()

const type = ref('')
const finishStatusOptions = ref([])
const shelvesOptions = ref([])
const formData = ref({
            name: '',
            imgUrl: "",
            bannerImgUrl: "",
            author: '',
            totalCnt: 0,
            maxFreeCnt: 0,
            price: 0,
            fullFlag: undefined,
            cpId: 0,
            cpUpdateTime: new Date(),
            protagonist: '',
            tag: '',
            intro: '',
            recommend: '',
            remarks: '',
            status: undefined,
            firstSid: 0,
            latestEpisode: '',
            createUid: 0,
            createTime: new Date(),
            updateUid: 0,
            updateTime: new Date(),
        })
// 验证规则
const rule = reactive({
               name : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               imgUrl : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               maxFreeCnt : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               price : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
})

const elFormRef = ref()

// 初始化方法
const init = async () => {
 // 建议通过url传参获取目标数据ID 调用 find方法进行查询数据操作 从而决定本页面是create还是update 以下为id作为url参数示例
    if (route.query.id) {
      const res = await findMovieAuto({ ID: route.query.id })
      if (res.code === 0) {
        formData.value = res.data.removie
        type.value = 'update'
      }
    } else {
      type.value = 'create'
    }
    finishStatusOptions.value = await getDictFunc('finishStatus')
    shelvesOptions.value = await getDictFunc('shelves')
}

init()
// 保存按钮
const save = async() => {
      elFormRef.value?.validate( async (valid) => {
         if (!valid) return
            let res
           switch (type.value) {
             case 'create':
               res = await createMovieAuto(formData.value)
               break
             case 'update':
               res = await updateMovieAuto(formData.value)
               break
             default:
               res = await createMovieAuto(formData.value)
               break
           }
           if (res.code === 0) {
             ElMessage({
               type: 'success',
               message: '创建/更改成功'
             })
           }
       })
}

// 返回按钮
const back = () => {
    router.go(-1)
}

</script>

<style>
</style>
